<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input
        v-model="userInfo.password"
        type="password"
        style="width: 300px"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { saveUserDetailById } from '@/api/employees.js'
import { getUserDetailById } from "@/api/user";
export default {
  data() {
    return {
      usid: this.$route.query.id,
      userInfo: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", triggler: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", triggler: "blur" },
        ],
      },
    };
  },
  created() {
    this.loadUserDetailById();
  },
  methods: {
    // 渲染表单
    async loadUserDetailById() {
      const res = await getUserDetailById(this.usid);
      console.log("getUserDetailById", res);
      this.userInfo = { ...res.data, password: "" };
    },
    // 提交按钮
    hSubmit() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.doEdit();
        }
      });
    },
    async doEdit() {
      try {
        const res = await saveUserDetailById(this.userInfo);
        console.log("saveUserDetailById", res);
        this.$message.success("修改成功");
        // 保存数据
        this.$router.back()
      } catch (err) {
        console.log("saveUserDetailById", err);
        this.$message.error("修改失败");
      }
    },
  },
};
</script>